package com.example.mycaculator;

import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import net.sourceforge.jeval.EvaluationException;
import net.sourceforge.jeval.Evaluator;


// Have class implement OnClickListener to have all the buttons listen to the same
// OnClick method on the class
public class MainActivity extends Activity  implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get reference to each of the buttons on the Layout
        Button bOne = (Button) findViewById(R.id.bnumber1);
        Button bZero = (Button) findViewById(R.id.bnumber0);
        Button bTwo = (Button) findViewById(R.id.bnumber2);
        Button bThree = (Button) findViewById(R.id.bnumber3);
        Button bFour = (Button) findViewById(R.id.bnumber4);
        Button bFive = (Button) findViewById(R.id.bnumber5);
        Button bSix = (Button) findViewById(R.id.bnumber6);
        Button bSeven = (Button) findViewById(R.id.bnumber7);
        Button bEigth = (Button) findViewById(R.id.bnumber8);
        Button bNine = (Button) findViewById(R.id.bnumber9);
        Button bDel  = (Button) findViewById(R.id.bdelete);
        Button bPlus = (Button) findViewById(R.id.bplus);
        Button bMinus = (Button) findViewById(R.id.bminus);
        Button bDiv = (Button) findViewById(R.id.bdiv);
        Button bMult = (Button) findViewById(R.id.bmult);
        Button bEq = (Button) findViewById(R.id.bequals);
        Button bDot = (Button) findViewById(R.id.bdot);

        //Set onclick listener for each of the buttons
        bOne.setOnClickListener(this);
        bTwo.setOnClickListener(this);
        bThree.setOnClickListener(this);
        bFour.setOnClickListener(this);
        bFive.setOnClickListener(this);
        bSix.setOnClickListener(this);
        bSeven.setOnClickListener(this);
        bEigth.setOnClickListener(this);
        bNine.setOnClickListener(this);
        bZero.setOnClickListener(this);
        bDel.setOnClickListener(this);
        bPlus.setOnClickListener(this);
        bMinus.setOnClickListener(this);
        bDiv.setOnClickListener(this);
        bMult.setOnClickListener(this);
        bEq.setOnClickListener(this);
        bDot.setOnClickListener(this);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    // OnClick method handles click events for all the buttons on the layout
    // depending on which button was clicked we get a different output on the
    // EditText view on the Layout.
    @Override
    public void onClick(View view) {

        EditText t = (EditText) findViewById(R.id.editText );
       if(view.getId() == R.id.bnumber1)
        {
         t.setText(t.getText().toString() + "1");
        }
       else if (view.getId() == R.id.bnumber2)
       {
         t.setText(t.getText().toString() + "2");
       }
       else if (view.getId() == R.id.bnumber3)
       {
           t.setText(t.getText().toString() + "3");
       }
       else if (view.getId() == R.id.bnumber4)
       {
           t.setText(t.getText().toString() + "4");
       }
       else if (view.getId() == R.id.bnumber5)
       {
           t.setText(t.getText().toString() + "5");
       }
       else if (view.getId() == R.id.bnumber6)
       {
           t.setText(t.getText().toString() + "6");
       }
       else if (view.getId() == R.id.bnumber7)
       {
           t.setText(t.getText().toString() + "7");
       }
       else if (view.getId() == R.id.bnumber8)
       {
           t.setText(t.getText().toString() + "8");
       }
       else if (view.getId() == R.id.bnumber9)
       {
           t.setText(t.getText().toString() + "9");
       }
       else if (view.getId() == R.id.bnumber0)
       {
           t.setText(t.getText().toString() + "0");
       }
       else if (view.getId() == R.id.bdelete)
       {
           t.getText().clear();
       }
       else if (view.getId() == R.id.bdot)
       {
           t.setText(t.getText().toString() + ".");
       }
       else if (view.getId() == R.id.bminus)
       {
           t.setText(t.getText().toString() + " - ");
       }
       else if (view.getId() == R.id.bmult)
       {
           t.setText(t.getText().toString() + " X ");
       }
       else if (view.getId() == R.id.bplus)
       {

           t.setText(t.getText().toString() + " + ");
       }
       else if (view.getId() == R.id.bdiv)
       {
           t.setText(t.getText().toString() + " / ");
       }


       else if (view.getId() == R.id.bequals)
       {
           Evaluator x = new Evaluator();

           try
           {
             // Need to replace the X with * or the expression will not compile and generate
             // and exception
              t.setText(x.evaluate(t.getText().toString().replace("X","*")).toString());
           }

           catch (EvaluationException e) {
                 e.printStackTrace();
       }
       }

    }
}
